package com.ptadmin.report.mws;

import java.time.LocalDate;
import java.util.GregorianCalendar;
import java.util.List;

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

import com.amazonaws.mws.MarketplaceWebService;
import com.amazonaws.mws.MarketplaceWebServiceClient;
import com.amazonaws.mws.MarketplaceWebServiceConfig;
import com.amazonaws.mws.MarketplaceWebServiceException;
import com.amazonaws.mws.model.IdList;
import com.amazonaws.mws.model.TypeList;
import com.ptadmin.report.ConfigurationManager;
import com.ptadmin.report.InputParameter;

public abstract class ReportWrapperBase {

	/************************************************************************
	 * Access Key ID and Secret Access Key ID, obtained from: http://aws.amazon.com
	 ***********************************************************************/
	String accessKeyId = ConfigurationManager.getInstance().getPropertiesConfig().getString("mws.accessKeyId");
	String secretAccessKey = ConfigurationManager.getInstance().getPropertiesConfig().getString("mws.secretAccessKey");
	String appName = ConfigurationManager.getInstance().getPropertiesConfig().getString("mws.appName");
	String appVersion = ConfigurationManager.getInstance().getPropertiesConfig().getString("mws.appVersion");
//	final String accessKeyId = "AKIAI2JEZAYVZZZ3GHGA";
//	final String secretAccessKey = "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";
//
//	final String appName = "ptadmin";
//	final String appVersion = "1.0.0";
//
//	protected String merchantId;
//	protected String sellerDevAuthToken;
//
//	protected RequestCommandBase command;
//	protected InputParameter inputParameter;
//	protected ReportParameter reportParameter;
//	protected Object request;
//	protected Object response;
	
	private static DatatypeFactory df = null;

	static {
		try {
			df = DatatypeFactory.newInstance();
		} catch (DatatypeConfigurationException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	private MarketplaceWebServiceConfig config = new MarketplaceWebServiceConfig();
	protected MarketplaceWebService service = null;

	public ReportWrapperBase() {
		config.setServiceURL(ServiceURL.US.getUrl());
		config.setConnectionTimeout(1000*3600);
		this.service = new MarketplaceWebServiceClient(accessKeyId, secretAccessKey, appName, appVersion, config);
	}
	
	public static IdList buildIdList(List<String> idList) {
		final IdList list = new IdList(idList);
		return list;
	}

	public static XMLGregorianCalendar buildMwsDate(LocalDate localDate) {
		XMLGregorianCalendar date = df.newXMLGregorianCalendar(new GregorianCalendar(localDate.getYear(), localDate.getMonthValue(), localDate.getDayOfMonth()));
		return date;
	}
	public static XMLGregorianCalendar buildMwsDate(int year, int month, int day) {
		XMLGregorianCalendar date = df.newXMLGregorianCalendar(new GregorianCalendar(year, month, day));
		return date;
	}

	public enum ServiceURL {
		US("https://mws.amazonservices.com/"), UK("https://mws.amazonservices.co.uk/"), Germany(
				"https://mws.amazonservices.de/"), France("https://mws.amazonservices.fr/"), Italy(
						"https://mws.amazonservices.it/"), Japan("https://mws.amazonservices.jp/"), China(
								"https://mws.amazonservices.com.cn/"), Canada(
										"https://mws.amazonservices.ca/"), India("https://mws.amazonservices.in/");

		private String url;

		private ServiceURL(String url) {
			this.url = url;
		}

		public String getUrl() {
			return this.url;
		}

		@Override
		public String toString() {
			return this.url;
		}
	}
}
